﻿{% extends "app/layout.html" %}
{% load myfilter %}
{% block style %}
<link rel="stylesheet" href="//cdn.staticfile.org/codemirror/5.48.2/codemirror.min.css">
{% ifequal p.source '洛谷' %}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0-rc.1/katex.min.css">
{% endifequal %}
{% endblock %}

{% block content %}
<div class="page-header">
    <h1>{{ p.title }}
    <small>
        {% for t in tags %}
        <span class="badge badge-info">{{ t.name }}</span>
        {% endfor %}
        <span class="badge badge-default">提交：{{ p.submission_number }}</span>
        <span class="badge badge-success">通过：{{ p.accepted_number }}</span>
        <span class="badge badge-warning">难度：{{ p.difficulty|difficulty_name }}</span>
        <span class="badge badge-error">时限：{{ p.time_limit }}ms</span>
        <span class="badge badge-info">内存：{{ p.memory_limit }}MB</span>
    </small>
    </h1>
    {% if user.is_superuser %}
    <div id="alltags">
        {% for t in alltags %}
        <label class="checkbox-inline"><input type="checkbox" value="{{ t.id }}">{{ t.name }}</label>
        {% endfor %}
    </div>
    {% endif %}
</div>
{% autoescape off %}
<div class="row" id="problem_content">
    <div class="panel panel-default">
        <div class="panel-heading"><h3 class="panel-title">题目描述</h3></div>
        <div class="panel-body">{{ p.description }}</div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading"><h3 class="panel-title">输入说明</h3></div>
        <div class="panel-body">{{ p.input_description }}</div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading"><h3 class="panel-title">输出说明</h3></div>
        <div class="panel-body">{{ p.output_description }}</div>
    </div>
    {% for sample in samples %}
    <div class="panel panel-default">
        <div class="panel-heading"><h3 class="panel-title">样例输入</h3></div>
        <div class="panel-body"><pre>{{ sample.input }}</pre></div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading"><h3 class="panel-title">样例输出</h3></div>
        <div class="panel-body"><pre>{{ sample.output }}</pre></div>
    </div>
    {% endfor %}
    <div class="panel panel-default">
        <div class="panel-heading"><h3 class="panel-title">提示</h3></div>
        <div class="panel-body">{{ p.hint }}</div>
    </div>
</div>
{% endautoescape %}
{% if islogin %}
<div class="row">
<div class="panel panel-default">
  <div class="panel-heading"><h3 class="panel-title">代码</h3></div>
  <div class="panel-body">
  <input type="hidden" name="problem_id" id="problem_id" value="{{ p.id }}">
  <div role="form">
{% if submission is None %}
    <textarea class="form-control"  id="code" name="code" rows="28">#include <iostream>
using namespace std;

int main()
{
   cout << "Hello World";
   return 0;
}</textarea>
{% else %}
    <textarea class="form-control"  id="code" name="code" rows="28" style="min-height: 600px">{{ submission.code }}</textarea>
{% endif %}
  <br />
  <div>
      <button id="postsubmission_button" type="button" class="btn btn-default">提交</button>
      <span id="postsubmission_msg"></span>
      <button id="refresh_submission_button" type="button" class="btn btn-default hidden">刷新</button> <span id="refresh_time"></span></div>
  </div>
  </div>
</div>
    <div class="panel panel-default">
        <div class="panel-heading"><h3 class="panel-title">测试结果</h3></div>
        <div class="panel-body"><div id="submission_result"></div></div>
    </div>
</div>


{% comment %}
<div role="form">
  <div class="form-group">
    <label for="code">讨论 <span id="postdiscuss_msg"></span></label><br />
    <textarea name="discuss" id="discuss" class="form-control" rows="16" style="min-width: 90%"></textarea>
  </div>
  <button id="postdiscuss_button" type="button" class="btn btn-default">提交</button>
</div>
{% endcomment %}

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-body">
				正在提交，请稍侯......
			</div>
		</div>
	</div>
</div>
{% endif %}
{% endblock %}

{% block scripts %}
{% if islogin %}
<script type="text/javascript">
{% if submission is None %}
    var s_result = {
        id: -1,
        result: null,
        info: null,
        statistic_info: null
    };
{% else %} 
    var s_result = {
        id: {{ submission.id }},
        result: {{ submission.result }},
        info: {% ifequal submission.info '' %}''{% else %}{{ submission.info|safe }}{% endifequal %},
        statistic_info: {% ifequal submission.statistic_info '' %}''{% else %}{{ submission.statistic_info|safe }}{% endifequal %}
    };
{% endif %}

function result_str(r){
    /*
    COMPILE_ERROR = -2
    WRONG_ANSWER = -1
    ACCEPTED = 0
    CPU_TIME_LIMIT_EXCEEDED = 1
    REAL_TIME_LIMIT_EXCEEDED = 2
    MEMORY_LIMIT_EXCEEDED = 3
    RUNTIME_ERROR = 4
    SYSTEM_ERROR = 5
    PENDING = 6
    JUDGING = 7
    PARTIALLY_ACCEPTED = 8
    */
    if(r==-2) return "编译出错";
    if(r==-1) return "答案错误";
    if(r==0) return "通过";
    if(r==1) return "超时";
    if(r==2) return "超时";
    if(r==3) return "内存超限";
    if(r==4) return "运行出错";
    if(r==5) return "系统出错";
    if(r==6) return "暂停";
    if(r==7) return "判别中";
    if(r==8) return "部分出错";
}
function display_result(){
    //submission_result
    if(s_result.id>0){
        html = "";
        if(s_result.result==0 || s_result.result==8){
            html += "<h3>"+result_str(s_result.result)+"</h3>";
            html += "<div>测试数据</div>";
            html += "<div><ol>";
            for(var i=0;i<s_result.info.data.length;i++){
                r = s_result.info.data[i];
                html += "<li>";
                // "test_case": "5", "signal": 0, "output": null, "memory": 3121152, "real_time": 3, "cpu_time": 0, "exit_code": 0, "error": 0, "output_md5": "f191a93972479e9df9b3f828891221f3", "result": 0
                html += " <span>"+result_str(r.result)+"</span> ";
                html += " <a href='/downloadcase/?case_id={{ p.test_case_id }}&filename="+r.test_case+".in'>输入文件</a> ";
                html += " <a href='/downloadcase/?case_id={{ p.test_case_id }}&filename="+r.test_case+".out'>输出文件</a> ";
                html += "</li>";
            }
            html += "</ol></div>";
        } else {
            html += "<h3>"+result_str(s_result.result)+"</h3>";
            if(s_result.statistic_info.err_info)
                html += "<div><pre>"+s_result.statistic_info.err_info+"</pre></div>";
        }
        $("#submission_result").html(html);
    } else {
        $("#submission_result").html("");
    }
}
</script>
{% endif %}
<script src="//cdn.staticfile.org/codemirror/5.48.2/codemirror.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.48.2/addon/mode/simple.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.48.2/mode/clike/clike.js"></script>

<script type="text/javascript">
    var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
	    lineNumbers: true,
	    matchBrackets: true,
	    mode: "text/x-c++src",
	    indentUnit: 4,
	    indentWithTabs: true,
    });

    function wait10s(){
        var time = 10;
        var time1=setInterval(function(){
           if(time==0){
              $("#refresh_submission_button").removeClass("disabled")
              $("#refresh_time").html("");
			  $("#postsubmission_button").removeAttr("disabled");
              clearInterval(time1);
			  $.ajax({
                url: "{% url 'getsubmission' %}?id=" + s_result.id + "&_t=" + (new Date().getTime()),
                type: "GET",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    s_result.result = data.data.result;
                    s_result.info = $.parseJSON(data.data.info);
                    s_result.statistic_info = $.parseJSON(data.data.statistic_info);
                    display_result();
                }
              });
           }else{
              $("#refresh_time").html(time);
              time--;
           }
        }, 1000);
    }

    $(document).ready(function () {
        $("#postsubmission_button").on('click', function () {
            $("#myModal").modal('show');
			$("#postsubmission_button").attr("disabled","true");
            $.ajax({
                url: "{% url 'postsubmission' %}",
                data: { problem_id: $("#problem_id").val(), code:editor.getValue() },
                type: "POST",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    s_result.id = data.data;
                    $("#postsubmission_msg").text(data.msg);
                    $("#myModal").modal('hide');
    
                    $("#refresh_submission_button").removeClass("hidden");
                    $("#refresh_submission_button").addClass('disabled');
                    wait10s();
                }
            });
        });

        $("#refresh_submission_button").on('click', function () {
            //getsubmission  参数  id  返回  data 'result', 'info', 'statistic_info'
            $.ajax({
                url: "{% url 'getsubmission' %}?id=" + s_result.id + "&_t=" + (new Date().getTime()),
                type: "GET",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    s_result.result = data.data.result;
                    s_result.info = $.parseJSON(data.data.info);
                    s_result.statistic_info = $.parseJSON(data.data.statistic_info);
                    display_result();

                    $("#refresh_submission_button").addClass('disabled');
                    wait10s();
                }
            });
        });

        display_result();

{% if user.is_superuser %}
var cked = {
    {% for t in tags %}
    "{{ t.id }}": "{{ t.name }}",
    {% endfor %}
    "none": ""
};

$("#alltags input").each(function() {
    if($(this).val() in cked){
        $(this).attr("checked",true);
    }
}); 

$("#alltags input").change(function() {
    let ac = $(this).is(':checked') ? "add" : "del";
    $.ajax({
        url: "{% url 'checktag' %}?tid=" + $(this).val() + "&pid={{ p.id }}&ac=" + ac,
        type: "GET",
        dataType: "json",
        success: function (data) {
            console.log(data);
        }
    });
});

{% endif %}


    });
</script>
{% endblock %}